package com.mobilesales.android.activities.clients;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.mobilesales.android.R;
import com.mobilesales.android.activities.ListModelActivity;
import com.mobilesales.android.adapters.ClientAdapter;
import com.mobilesales.android.database.ClientManager;
import com.mobilesales.android.model.clients.Client;
import com.mobilesales.android.util.Constants;
import com.mobilesales.android.util.FilterFieldWatcher;
import com.mobilesales.android.util.MessageBox;

public class Clients extends ListModelActivity<Client> {
	private EditText searchClientField;
	private ClientAdapter itemsAdapter;
	private FilterFieldWatcher filter;
	
	private int requestCode = 0;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.client_list);
		this.searchClientField = ((EditText) findViewById(R.id.clientFilterText));
		loadItems();
		Bundle bundle = this.getIntent().getExtras();
		if (bundle!=null && bundle.containsKey(Constants.RequestCode)) {
			requestCode = bundle.getInt(Constants.RequestCode);
		}
	}

	private void loadItems() {
		List<Client> items = getItems();
		if (items != null)
			setItemsSource(items);
		else
			MessageBox.show(this, getString(R.string.ErrorClientList));
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position,
			long id) {
		super.onListItemClick(listView, view, position, id);

		Client client = (Client) this.itemsAdapter.getItem(position);
		if (requestCode == Constants.RequestCodes.ChooseClient) {
			Bundle bundle = new Bundle();
			bundle.putInt(Constants.ClientId, client.Id);
			Intent intent = new Intent();
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finish();
		}
		else {
			Intent intent = new Intent(this, DetailsClient.class);
			intent.putExtra(Constants.ClientId, client.Id);
			startActivityForResult(intent, 0);
		}
	}

	public void addClient(View v) {
		startActivityForResult(new Intent(this, StoreClient.class), 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		loadItems();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (this.filter != null)
			this.searchClientField.removeTextChangedListener(this.filter);
	}

	public void refresh() {
		this.getHandler().post(new Runnable() {
			public void run() {
				loadItems();
			}
		});
	}

	@Override
	protected void setItemsSource(List<Client> source) {
		this.itemsAdapter = new ClientAdapter(this,
				R.layout.client_list_item, source);
		setListAdapter(this.itemsAdapter);
		this.filter = new FilterFieldWatcher(this.itemsAdapter);
		this.searchClientField.addTextChangedListener(this.filter);
	}

	@Override
	protected List<Client> getItems() {
		ClientManager manager = new ClientManager(this);
		List<Client> result = manager.GetClientList();
		manager.Close();
		return result;
	}
}






